import { DELETE_TODOLIST_ITEM, ADD_TODOLIST_ITEM } from "./actionType";
let todoListData = localStorage.getItem("todoListData");
todoListData = (todoListData && JSON.parse(todoListData)) || [];
function todoListReducer(
  state = {
    todoListData,
  },
  action
) {
  let newState = state;
  switch (action.type) {
    case ADD_TODOLIST_ITEM:
      // 进行深拷贝state，不能直接修改state
      newState = JSON.parse(JSON.stringify(newState));
      newState.todoListData.push(action.data);
      return newState;
    case DELETE_TODOLIST_ITEM:
      newState = JSON.parse(JSON.stringify(newState));
      newState.todoListData = newState.todoListData.filter(
        (item) => item.id !== action.data
      );
      return newState;
    default:
      return newState;
  }
}
export default todoListReducer;
